package com.leyou.item.service;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.service
 * @date 2019/6/22 19:45
 * @description:
 **/
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 品牌查询
     * @param pid
     * @return
     */
    public List<CategoryDTO> queryIsParent(Long pid) {

        //设置查询条件，mapper会把对象中非空属性作为查询条件
        Category category = new Category();

        category.setParentId(pid);

        List<Category> categories = categoryMapper.select(category);

        //非空判断
        if(CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        //使用自定义工具类，把Category集合转化为DTO集合

        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);

    }

    /**
     * 修改品牌
     * @param brandId
     * @return
     */
    public List<CategoryDTO> queryByBrandId(Long brandId) {
        //根据品牌Id查询数据
        List<CategoryDTO> list= categoryMapper.queryByBrandId(brandId);

        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    /**
     * 根据cids集合查询分类数据
     * @param cids
     * @return
     */
    public List<CategoryDTO> queryCategoryByCids(List<Long> cids){

        List<Category> list = categoryMapper.selectByIdList(cids);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }
}
